package ink.lovejinhu.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jinhu
 * created 2021-07-08 9:15
 */
public class Solution_0228 {
    /**
     * 汇总区间
     * 返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表。也就是说，nums 的每个元素都恰好被某个区间范围所覆盖，并且不存在属于某个范围但不属于 nums 的数字 x
     * nums有序
     *
     * @param nums
     * @return
     */
    public List<String> summaryRanges(int[] nums) {
        ArrayList<String> temp = new ArrayList<>();
        for (int i = 0; i < nums.length; ) {
            int start = i;
            int end = i;
            int j = 0;
            while (nums[i] + j == nums[i + j]) {
                end = i + j;
                j++;
                if (i + j >= nums.length) {
                    break;
                }
            }
            i = i + j -1+1;
            if (end - start == 0) {
                temp.add(nums[start] + "");
            } else {
                temp.add(nums[start] + "->" + nums[end]);
            }
        }
        return temp;
    }

    public static void main(String[] args) {
        Solution_0228 solution_0228 = new Solution_0228();
        int[] param = {0, 1, 2, 4, 5, 7};
        solution_0228.summaryRanges(param);
        System.out.println(solution_0228.summaryRanges(param));
    }
}
